﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.IO;

using Microsoft.VisualBasic;

using System.Net;
using System.Net.Sockets;
using System.Threading;

using Lists;
using Sim;
using Users;
using Sim.Lists;

namespace ServerApplication
{
    class ServerApplications
    {
        ASCIIEncoding byteEncode = new ASCIIEncoding();
        private UdpClient udpTransmit = new UdpClient(25002);
        private UdpClient udpReceive = new UdpClient(25001);
        IPEndPoint rxPoint = new IPEndPoint(IPAddress.Any, 25001);
        IPEndPoint bcPoint = new IPEndPoint(IPAddress.Broadcast, 25002);
        private byte[] byteMsgReceive;
        private byte[] byteMsgSend;
        string serverIP = IPList.GetPublicIP();

        public void Broadcast()
        {
            while (true)
            {
                byteMsgSend = byteEncode.GetBytes(serverIP + "#ServerBroadcast");

                udpTransmit.Send(byteMsgSend, byteMsgSend.Length, bcPoint);
                Thread.Sleep(300);
            }
        }

        public void Receive()
        {
            while (true)
            {
                byteMsgReceive = udpReceive.Receive(ref rxPoint);

                string[] split = byteEncode.GetString(byteMsgReceive).Split('#');
                string funcCall = split[1];

                Console.WriteLine(byteEncode.GetString(byteMsgReceive));

                if (funcCall == "Login")
                {
                    Console.WriteLine("Trying to login");
                    receiveLogin();
                }
                else if (funcCall == "Login success")
                {
                    sendContestInformation(0);
                    Console.WriteLine("Sending contest information");
                }
                else if (funcCall == "Sendscore")
                    receiveScore();
                else if (funcCall == "RegisterUser")
                    recieveUser();
                else if (funcCall == "SendDive")
                    recieveDive();
            }
            return;
        }

        public void sendContestInformation(int index)
        {
            string[] split = byteEncode.GetString(byteMsgReceive).Split('#');
            string ip = split[0];

            TournamentList.DeserializeFromXML();
            for (int i = 0; i < TournamentList.tournamentList.Count; i++)
            {
                if (i == index)
                {
                    Transmit(TournamentList.tournamentList[i].title + TournamentList.tournamentList[i].sex + Convert.ToString(TournamentList.tournamentList[i].date), ip);
                }
            }
        }

        public int recieveDive()
        {
            string[] split = byteEncode.GetString(byteMsgReceive).Split('#');
            string ip = split[0];

            Transmit("Sent", ip);
            return 0;
        }


        public int recieveUser()
        {
            string[] split = byteEncode.GetString(byteMsgReceive).Split('#');
            string ip = split[0];
            string firstName = split[2];
            string lastName = split[3];
            int age = Convert.ToInt32(split[4]);
            string nation = split[5];
            string sex = split[6];
            int level = Convert.ToInt32(split[7]);
            string username = split[8];
            string password = split[9];
            User RegisterUser = new User(firstName, lastName, age, nation, sex, level, username, password);
            int Register = UserList.AddUser(RegisterUser);
            if (Register == 0)
                Transmit("Server#Registration#Failed", ip);
            else if(Register == 1)
                Transmit("Server#Registration#Success", ip);
            //else if(Register == 2)
            //    Transmit("Username is too short", ip);
            //else if(Register == 3)
            //    Transmit("Password is too short", ip);
            return 0;
        }
        public int receiveScore()
        {
            string[] split = byteEncode.GetString(byteMsgReceive).Split('#');
            string ip = split[0];

            Transmit("Server#Sendscore#Success", ip);
            return 0;
        }

        public int receiveLogin()
        {
            string[] split = byteEncode.GetString(byteMsgReceive).Split('#');
            string ip = split[0];

            if (LoginValidation(byteEncode.GetString(byteMsgReceive)) == 1)
            {
                Transmit("Server#Login#Success", ip);
                return 1;
            }
            else
            {
                Transmit("Server#Login#Failed", ip);
                return 0;
            }
        }

        public void Transmit(string msg, string ip)
        {
            string transmittingIP = ip;
            byteMsgSend = byteEncode.GetBytes(msg);
            udpTransmit.Send(byteMsgSend, byteMsgSend.Length, transmittingIP, 25002);
        }

        int LoginValidation(string line)
        {
            string[] split = line.Split('#');
            string username = split[2];
            string password = split[3];
            Login login = new Login();
            return login.Validate(username, password);
        }



    }
}
